package com.github.McPlugin;

import com.github.McPlugin.dao.MySQLManager;
import com.github.McPlugin.command.*;
import com.github.McPlugin.event.BlocksEvent;
import com.github.McPlugin.event.EntitysEvent;
import com.github.McPlugin.event.PlayersEvent;
import com.github.McPlugin.sql.SQLiteManager;
import com.github.McPlugin.util.SavaItemStack;
import com.github.McPlugin.util.Task.ListType;
import com.github.McPlugin.util.Task.TasksTool;
import com.github.McPlugin.util.VaultUtil;
import com.github.McPlugin.vexview.GuiEvent;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

import java.io.File;

public class main extends JavaPlugin
{
    public static JavaPlugin plugin;
    public static FileConfiguration config;

    @Override
    public void onEnable()
    {
        plugin=this;

        //config文件夹是否存在，不存在则创建config文件夹
        if(!getDataFolder().exists())
            getDataFolder().mkdir();
        File file = new File(getDataFolder(),"config.yml");
        if(!file.exists())
            saveDefaultConfig();
        reloadConfig();
        config = getConfig();
        //
        getLogger().info("开始连接数据库");
        new BukkitRunnable() {
            @Override
            public void run() {
                SQLiteManager.get().connectSQLiteDB();
                getLogger().info("数据存储成功");
                MySQLManager.get().enableMySQL();
                //初始化任务列表
                TasksTool.init();
            }
        }.runTaskAsynchronously(this);
        //创建新线程用于数据库操作
        new BukkitRunnable()
        {
            @Override
            public void run()
            {
                MySQLManager.get().enableMySQL();
            }
        }.runTaskAsynchronously(this);



        //vault
        if(VaultUtil.setupEconomy())
        {
            getLogger().info("-----------------------------");
            getLogger().info("Vault启动成功");
            getLogger().info("-----------------------------");
        }
        else
        {
            getLogger().info("Vault启动失败");
        }

        //vexview
        if(Bukkit.getPluginManager().isPluginEnabled("VexView")){
            getLogger().info("已经成功启动Vex界面");
        }else{
            getLogger().info("启动Vex界面失败");
        }

        //命令注册
        this.getCommand("help").setExecutor(new HelpCommand());
        this.getCommand("sign").setExecutor(new SignCommand());
        this.getCommand("shop").setExecutor(new ShopCommand());
        this.getCommand("mission").setExecutor(new MissionCommand());
        this.getCommand("activity").setExecutor(new ActivityCommand());
        this.getCommand("title").setExecutor(new TitleCommand());
        this.getCommand("transfer").setExecutor(new TitleCommand());
        this.getCommand("person").setExecutor(new TitleCommand());



        //事件监听注册
        Bukkit.getPluginManager().registerEvents(new GuiEvent(), this);
        Bukkit.getPluginManager().registerEvents(new PlayersEvent(), this);
        Bukkit.getPluginManager().registerEvents(new EntitysEvent(), this);
        Bukkit.getPluginManager().registerEvents(new BlocksEvent(), this);



        //插件启动信息
        getLogger().info("-----------------------------");
        getLogger().info(" 起名废v2组 MinecraftPlugin 启动  ");
        getLogger().info("-----------------------------");

        super.onEnable();
    }

    @Override
    public void onDisable()
    {
        //任务信息存储到数据库
        TasksTool.toDB(TasksTool.list_DailyTask, ListType.DAILYTASK_LIST);
        TasksTool.toDB(TasksTool.list_finished, ListType.FINISHED_LIST);
        TasksTool.toDB(TasksTool.list_tasks, ListType.UNFINISHED_LIST);

        //插件关闭信息
        getLogger().info("-----------------------------");
        getLogger().info("                            ");
        getLogger().info(" 起名废v2组 MinecraftPlugin 关闭  ");
        getLogger().info("                            ");
        getLogger().info("-----------------------------");

        //断开数据库连接
        MySQLManager.get().shutdown();
        super.onDisable();
    }

}
